<?php
/**
 * Plugin_PHPDoctrine_Common
 * 
 * class for PHPDoctrine plugin loading
 * PHPDoctrine - fast and flexible ORM. @link http://www.phpdoctrine.org
 * About this and other my phpxcore plugins you can read at @link http://www.feodosian.com/phpxcore-plugins
 * Google code @link http://code.google.com/p/mixadior-phpxcore-plugins/
 * 
 * @package PHPXCore
 * @version 0.0.1.0-doctrine0.11.0RC2
 * @author Diordienko Mykhail (i@feodosian.com)
 *  
 *
 */

class Plugins_PHPDoctrine_Common extends XCore_Plugin_Abstract 
{
    public $_pluginPath = null;
    public $_modelsPath = null;
    public $_config = null;    
    public function __construct($config = null)
    {
        $this->_config = $config;
        $this->_pluginPath = dirname(__FILE__);
    }
    public function init($event = null, $config = null)
    {       
        switch ($event) {
            case 'get':
                break;                                          
            default:
                $this->_initIncludePathes();
                $this->_initDefaultConnection();  
        }
        
    }
    public function _initDefaultConnection()
    { 
                  
        if ($this->_config['connection']['@']['enabled']) {
            $dsn      = $this->_config['connection']['dsn'];
            $username = $this->_config['connection']['username'];
            $password = $this->_config['connection']['password'];      
            $charset  = $this->_config['connection']['charset']; 
            $connectionName = $this->_config['connection']['@']['name'];        
        }    
        else {    
            $pdoDriver = XCore::$Config['database']['pdo_driver'];
            $username = XCore::$Config['database']['params']['username'];
            $password = XCore::$Config['database']['params']['password'];
            $host = XCore::$Config['database']['params']['host'];
            $dbname = XCore::$Config['database']['params']['dbname'];
            $charset = XCore::$Config['database']['charset'];        
            $dsn = $pdoDriver.':dbname='.$dbname.';host='.$host;   
            $connectionName = "defaultConnection";     
        }    
        $pdoConnection = new PDO($dsn,$username,$password);        
        Doctrine_Manager::connection($pdoConnection, $connectionName);
        $manager = Doctrine_Manager::getInstance();
        $manager->getCurrentConnection()->setCharset($charset);
    }    
    public function _initIncludePathes()
    {
        set_include_path($this->_pluginPath.DS.'doctrineLib'.DS.'Doctrine-0.11.0'.DS.'lib' 
        . PATH_SEPARATOR . $this->_pluginPath.DS.'doctrineData'.DS.'models'        
        . PATH_SEPARATOR . $this->_pluginPath.DS.'doctrineData'.DS.'models'.DS.'generated'
        . PATH_SEPARATOR . get_include_path()); 
        XCore_Registry::set('doctrine_config', array(
            'data_fixtures_path'  =>  $this->_pluginPath.DS.'doctrineData'.DS.'fixtures',
            'models_path'         =>  $this->_pluginPath.DS.'doctrineData'.DS.'models',
            'migrations_path'     =>  $this->_pluginPath.DS.'doctrineData'.DS.'migrations',
            'sql_path'            =>  $this->_pluginPath.DS.'doctrineData'.DS.'sql',
            'yaml_schema_path'    =>  $this->_pluginPath.DS.'doctrineData'.DS.'schema'
        )); 
    }    
}